<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 9.<对象&字符串练习>对象转字符串
        const input1 = {
            name: 'zhangsan',
            age: 18,
            sex: 'nan',
        };
        const output1 = 'name=zhangsan&age=18&sex=nan';

        // function obj2str (input) {
        //     const res = [];
        //     for (const key in input) {
        //        const tmp = [key,input[key]].join('=');
        //        res.push(tmp);
        //     }
        //     const str = res.join('&');
        //     return str;
        // }
        // console.log(obj2str(input1));

        // ⭐
        function obj2str (input) {
            return Object.keys(input).map(k => `${ k }=${ input[k]}`).join('&');
        }
        console.log(obj2str(input1));


        // 10.<对象&字符串练习>字符串转对象
        const input2 = 'name=zhangsan&age=18&sex=nan&height=180';
        const output2 = {
            name: 'zhangsan',
            age: '18', // 这里是字符串的18
            sex: 'nan',
            height: '180',
        }
        // function str2obj (input) {
        //     const res = {};
        //     const arr = input.split('&');
        //     for (const item of arr) {
        //         const littleIetm = item.split('=');
        //         const k = littleIetm[0];
        //         const value = littleIetm[1];
        //         res[k] = value;
        //     }
        //     return res;
        // }
        // console.log(str2obj(input2));

        
        // ⭐
        function str2obj (input) {
            const res = {};
            for (const item of input.split('&')) {
                const [key, value] = item.split('=');
                res[key] = value;
            }
            return res;
        }
        console.log(str2obj(input2));

        // 11. <对象&字符串练习>字符串转对象
        const input3 = 'name=zhangsan&age=18&sex=nan&height=180';
        const output3 = {
            name: 'zhangsan',
            age: 18, // 这里是数字的18
            sex: 'nan',
            height: 180,
        };
        // function str2obj1 (input) {
        //     const res = {};
        //     const arr = input.split('&');
        //     for (const item of arr) {
        //         const littleIetm = item.split('=');
        //         const k = littleIetm[0];
        //         const value = littleIetm[1];
        //         const ResValue = isNaN(value) ? value : Number(value);
        //         res[k] = ResValue;
        //     }
        //     return res;
        // }
        // console.log(str2obj1(input3));

        // ⭐
        function str2obj1 (input) {
            const res = {};
            for (const item of input.split('&')) {
                const [key, value] = item.split('=');
                const ResValue = isNaN(value) ? value : Number(value);
                res[key] = ResValue;
            }
            return res;
        }
        console.log(str2obj1(input3));
    </script>
</body>
</html>                  